/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.facade.info.partner;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import info.peigen.hotpot.common.core.enums.YesOrNo;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import lombok.experimental.SuperBuilder;

import java.util.List;

import static info.peigen.hotpot.common.core.enums.YesOrNo.N;
import static info.peigen.hotpot.common.core.enums.YesOrNo.Y;

/**
 * <b>(PartnerPermissionInfo)</b>
 * 合作伙伴权限
 * 这里以json的方式存储,可随意增加
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/7/28
 */
@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Accessors(chain = true)
public class PartnerPermissionInfo {
    /** 可登录 */
    @Builder.Default
    YesOrNo login              = N;
    /** 可修改登录密码 */
    @Builder.Default
    YesOrNo editLoginPassword  = N;
    /** 可支付 */
    @Builder.Default
    YesOrNo pay                = N;
    /** 可修改交易密码 */
    @Builder.Default
    YesOrNo editPayPassword    = N;
    /** 是否发送30天证件过期提醒 */
    @Builder.Default
    YesOrNo sendThirtyReminder = Y;
    /** 是否发送90天证件过期提醒 */
    @Builder.Default
    YesOrNo sendNinetyReminder = Y;
    /** 允许调用的ApiCode */
    List<String> enableApiCodes;
    /** 是否开启api权限校验模式 */
    @Builder.Default
    YesOrNo isEnableValidation = Y;

    public static PartnerPermissionInfo genPartnerInitPermission() {
        return PartnerPermissionInfo.builder().build();
    }

    public YesOrNo isSendThirtyReminder() {
        return sendNinetyReminder;
    }

    public YesOrNo isSendNinetyReminder() {
        return sendNinetyReminder;
    }

    public YesOrNo isLogin() {
        return login;
    }

    public YesOrNo isEditLoginPassword() {
        return editLoginPassword;
    }

    public YesOrNo isPay() {
        return login;
    }

    public YesOrNo idEditPayPassword() {
        return editPayPassword;
    }

    public void check() {
        Assert.isTrue(ObjectUtil.isAllNotEmpty(login, pay, sendThirtyReminder, sendNinetyReminder), "权限不得为空!");
    }

    public boolean permissionAllClose() {
        List<YesOrNo> allPermission = Lists.newArrayList(login, editLoginPassword, pay, editPayPassword, sendThirtyReminder, sendNinetyReminder);
        return allPermission.stream().noneMatch(p -> p == Y);
    }

    public boolean isEnableApiCode(String apiCode) {
        return CollectionUtil.contains(enableApiCodes, apiCode);
    }
}
